package Monster;

import java.awt.Color;

import MainClass.Game;
import SubClasses.CellData;
import SubClasses.Direction;
import SubClasses.TypeOfVar;

public class Inside extends Monster
{
	public Inside(int xPos,int yPos,Direction x,Direction y,int s, int q)
	{
		Pos_X = xPos;
		Pos_Y= yPos;
		DirectionAxisX=x;
		DirectionAxisY=y; 
		Step=s; 
		Quantum=q; 
		type = TypeOfVar.FrameNotFill;
		ascii = '@';
		Forg = Color.BLACK;	
		
		game = Game.GameBuilder();
	}
	
	public Inside(int s)
	{
		game = Game.GameBuilder();
		
		int pos[] = game.randomLocationInside();
		Pos_X = pos[0];
		Pos_Y=pos[1];
		
		DirectionAxisX=game.GetUpDounDirection();
		DirectionAxisY=game.GetLeftRightDirection(); 
		
		Step=Quantum=s;
		//sQuantum=++startStep%Step;
		
		ascii = '@';
		Forg = Color.BLACK;		
	}
	@Override
	public void UpdateMonstersPosition() 
	{
		int newX[] = new int[8];
		int newY[] = new int[8];
		
		
		if (Quantum  > 0 ) 
		{
		   (Quantum )--;
		   return;
		}
		Quantum  = Step ;
		
		createAllPossibleOption(newX, newY);

		int i;
		for(i=0;i<8;i++)
		{
			if(!game.CheckIndax(newX[i], newY[i])) continue;
			if(game.isFrameFillIndex(newX[i], newY[i])) continue;
			if(game.isMonsterIndex(newX[i], newY[i])) continue; 
			if(game.isChasingLineIndex(newX[i], newY[i])) continue;
			if(game.isNewLineIndex(newX[i], newY[i])) 
			{
				game.user.startChase(newX[i], newY[i]);
				continue;
			}
			if(game.isUserIndex(newX[i], newY[i])) game.strike();
			
			break;
		}
		if(i==8)
		{
			return;
		}
		//System.out.println("i = " + i );
		 
		this.DirectionAxisX = NewDirectionAxisX(i);
		this.DirectionAxisY = NewDirectionAxisY(i);
		
		game.SetInBoardValues(Pos_X,Pos_Y,null,TypeOfVar.Empty,TypeOfVar.FrameNotFill);
		Pos_X = newX[i];
		Pos_Y= newY[i];
		//System.out.println(" Conse x = " + Pos_X + " Y = " + Pos_Y);
		game.SetInBoardValues(Pos_X,Pos_Y,this,TypeOfVar.Monster,TypeOfVar.FrameNotFill);
			
	}


	
	private boolean isLegalPosition(int i,int j)
	{
		if(game.CheckIndax(i, j) && game.isEmptyIndex(i, j) && game.isFrameFillIndex(i, j))
			return true;
		return false;
	}
	
	/*public void UpdateMonstersPosition() 
	{
		int newX = Pos_X + DirectionAxisX.GetValue() ;
		int newY = Pos_y + DirectionAxisY.GetValue();
		
		game.SetInBoardValues(Pos_X,Pos_y,null,TypeOfVar.Empty,TypeOfVar.FrameNotFill);
		
		System.out.println("x = " + Pos_X + " Y = " + Pos_y);
		//check if the index is OK
		if(game.CheckIndax(newX, newY)==false)
		{
			return;
		}
		// if the new index is empty, move the monster.
		if( CheckPosition(newX, newY))
		{
			Pos_X = newX;
			Pos_y= newY;
		}
		// the new position is not good
		else 
		{
			if( game.isFrameFillIndex(Pos_X, newY) &&  game.isFrameFillIndex(newX, Pos_y))
			{
				DirectionAxisX = DirectionAxisX.SwapDir();
				DirectionAxisY = DirectionAxisY.SwapDir();
			}
			else if( game.isFrameFillIndex(Pos_X, newY) )
			{
				DirectionAxisY = DirectionAxisY.SwapDir();
			}
			else if( game.isFrameFillIndex(newX, Pos_y))
			{
				DirectionAxisX = DirectionAxisX.SwapDir();
			}
			Pos_X = Pos_X + DirectionAxisX.GetValue();
			Pos_y = Pos_y + DirectionAxisY.GetValue();		
		}
		if(game.isUserIndex(newX, newY))
		{
			
		}
		game.SetInBoardValues(Pos_X,Pos_y,this,TypeOfVar.Monster,TypeOfVar.FrameNotFill);
			
	}
	*/
	/*private boolean CheckPosition(int i,int j)
	{
		if( game.isEmptyIndex(i, j) && game.isFrameNotFillIndex(i, j))
			return true;
		return false;
	}*/



	
	/*
	 void MoveInsideDiagonal(int indexInMonsterArray) 
{
	int nextIndex = MonstersArray[indexInMonsterArray].indexPosition + MonstersArray[indexInMonsterArray].lastDirectionAxisX + MonstersArray[indexInMonsterArray].lastDirectionAxisY;
	int nextIndexX = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisX;
	int nextIndexY = MonstersArray[indexInMonsterArray].indexPosition+MonstersArray[indexInMonsterArray].lastDirectionAxisY;
	int saveLastPositionIndex = MonstersArray[indexInMonsterArray].indexPosition;
	if (indexInMonsterArray>= NumberOfMonsters) return;
	switch (TemporaryScreen[nextIndex+1])
	{
		case NEW_LINE_BLOCK_COLOR:
			startChase(nextIndex); // no break - we set parameters for chase and bounce like from a wall;
		case FRAME_FILL_COLOR:// The next if: checks if the corner is taken but not the sides - 
		//in this case we should come back the way we came (we hit the outside of a corner)
			if (isGiftAscii(nextIndex)==1) 
			{
				MonstersArray[indexInMonsterArray].step = (MonstersArray[indexInMonsterArray].step>0?MonstersArray[indexInMonsterArray].step>0:0);
				DeleteGift();
			}
			if ((TemporaryScreen[nextIndexX+1] != FRAME_FILL_COLOR) && (TemporaryScreen[nextIndexY+1] != FRAME_FILL_COLOR) )  
				{
					MonstersArray[indexInMonsterArray].lastDirectionAxisX = (MonstersArray[indexInMonsterArray].lastDirectionAxisX==LEFT)?RIGHT:LEFT;
					MonstersArray[indexInMonsterArray].lastDirectionAxisY = (MonstersArray[indexInMonsterArray].lastDirectionAxisY==UP)?DOWN:UP;
				}
				else
				{
					if (TemporaryScreen[nextIndexX+1] == FRAME_FILL_COLOR)
					{
						MonstersArray[indexInMonsterArray].lastDirectionAxisX = (MonstersArray[indexInMonsterArray].lastDirectionAxisX==LEFT)?RIGHT:LEFT;
					}
					if (TemporaryScreen[nextIndexY+1] == FRAME_FILL_COLOR)
					{	
						MonstersArray[indexInMonsterArray].lastDirectionAxisY = (MonstersArray[indexInMonsterArray].lastDirectionAxisY==UP)?DOWN:UP;
					}
				}
				break;
		case USER_COLOR|FRAME_NOT_FILL_COLOR:
			if( (TemporaryScreen[nextIndex]==USER) )
					strike();
			return;

		// if we hit a monster we continue on the same Y dirextion buy change the X direction.
		case MONSTER_COLOR|FRAME_NOT_FILL_COLOR:
			MonstersArray[indexInMonsterArray].lastDirectionAxisX = (MonstersArray[indexInMonsterArray].lastDirectionAxisX==LEFT)?RIGHT:LEFT;
			MonstersArray[indexInMonsterArray].lastDirectionAxisY = (MonstersArray[indexInMonsterArray].lastDirectionAxisY==UP)?DOWN:UP;		
		break;
	}
	nextIndex = MonstersArray[indexInMonsterArray].indexPosition + MonstersArray[indexInMonsterArray].lastDirectionAxisX + MonstersArray[indexInMonsterArray].lastDirectionAxisY;
	MonstersArray[indexInMonsterArray].indexPosition= nextIndex;
	if(saveLastPositionIndex != MonstersArray[indexInMonsterArray].indexPosition)
	{
		// set the old value of the Square in the screen
		TemporaryScreen[saveLastPositionIndex] = MonstersArray[indexInMonsterArray].lastPositionAscii;
		TemporaryScreen[saveLastPositionIndex+1] = MonstersArray[indexInMonsterArray].lastPositionAttribute;
		//save the current ascii and Attribut
		if (isMonsterAscii(TemporaryScreen[MonstersArray[indexInMonsterArray].indexPosition]) == 0 )
		{
			MonstersArray[indexInMonsterArray].lastPositionAscii = TemporaryScreen[MonstersArray[indexInMonsterArray].indexPosition];
			MonstersArray[indexInMonsterArray].lastPositionAttribute = TemporaryScreen[MonstersArray[indexInMonsterArray].indexPosition+1];
		}
		//place monster on board
		TemporaryScreen[MonstersArray[indexInMonsterArray].indexPosition] = INSIDE_DIAGONAL_MONSTER;
		TemporaryScreen[MonstersArray[indexInMonsterArray].indexPosition+1] =  MONSTER_COLOR|TemporaryScreen[MonstersArray[indexInMonsterArray].indexPosition+1];
	} //changed position
}
	 */



}
